var app=getApp(),
a=app.requirejs('core'),
b = app.requirejs('api/kjb');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    interval: 5000,
    duration: 1000,
    popup: false,
    countDownDay: 0,
    countDownHour: 0,
    countDownMinute: 0,
    countDownSecond: 0,
    show_time: true,
    display: false,
  },

  onGotUserInfo: function (q) {
    var that = this,
        e = app.getCache("userinfo");

    if (e) {
      return;
    }
    app.getUserInfo(q.detail.userInfo, function (t) {
      if (t != 1000) {
        var ucache = getApp().getCache("userinfo");
        if (ucache) {
          getCurrentPages()[getCurrentPages().length - 1].onLoad();
          that.setData({
            display: false,
            getPhoneAuth: getApp().globalData.getphone
          })
        }
      } else {
        that.setData({
          display: true
        })
      }

    }, q.detail.encryptedData, q.detail.iv);

  },

  // 取消登录
  cancel_auth: function () {
    this.setData({
      display: false
    })
  },

  // 获取用户手机号
  getMobile: function (e) {
    app.globalData.getphone = false;
    this.setData({
      getPhoneAuth: false
    })
    app.getPhoneNumber(e, this, 3);
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    var id = options.bargain_id,
    e = app.getCache("userinfo");
    var that = this;
    if (!e.nickName && !e.avatarUrl) {
      getApp().authSwitch(function(res){
        if (res == 1) {
          that.setData({
            display: true
          })
        } else {
          that.setData({
            display: false
          })
        }
      })
    }

    this.setData(options);
    this.detail();
  },

  detail: function () {
    var that = this,
      uid = that.data.uid,
      bargain_id = that.data.bargain_id;
    b.kj_info(bargain_id, uid, that, function (t) {
      that.setData(t)
      //倒计时
      if (t.bargain_info.end_time) {
        var time = t.bargain_info.end_time;
        b.Countdown(time, function (i) {
          that.setData(i)
        })
      }
    })
  },

  /**
   * 回到名片页
   */
  toCard: function () {
    a.jump('/yb_mingpian/pages/card/index', 3);
  },

  tankuang: function () {
    this.setData({
      popup: false
    })
  },
  bargain_help: function (e) {
    var that = this,
    id=that.data.id,
    uid=that.data.uid;
    var formId = e.detail.formId
    // formid =
    if (!that.data.show_time){
      a.alert('该活动已经结束');
      return;
    }
    if (that.data.bargain_info.bargain_inventory<1){
      a.alert('库存不足');
      return;
    }
    b.BargainHelp(id, uid, formId,function(t){
      console.log(t)
      that.setData(t)
      that.detail()
    })
  },
  /**
   * 发起砍，直接购
   */
  shoping: function (e) {
    var that = this,
      id = a.pdata(e).id;
    if (!that.data.show_time) {
      a.alert('该活动已经结束')
      return false;
    }
    if (that.data.bargain_info.bargain_inventory < 1) {
      a.alert('库存不足')
      return false;
    }
    wx.navigateTo({
      url: '/yb_mingpian/pages/kanjia/discount_info/index?id=' + id
    })
    
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.detail();
    wx.stopPullDownRefresh();
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  }
})